﻿using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;

namespace Core.Res
{
    // ReSharper disable once InconsistentNaming
    public partial class WXLoader
    {
        public class Bundle : LoadAwaiter
        {
            private readonly WXLoader owner;

            public AssetBundle Ab { get; private set; }
            private BundleInfo bundleInfo;

            public Bundle(WXLoader loader) : base(loader)
            {
                this.owner = loader;
            }

            public void SetInfo(string bundle_name)
            {
                Name = bundle_name;
                bundleInfo = owner.ResDict.GetBundle(bundle_name);
            }

            public override void Start()
            {
                if (owner.bundles.ContainsKey(Name))
                {
                    State = ELoadState.Complete;
                    return;
                }
                State = ELoadState.Loading;
                Progress = 0;

                var path = Path.Combine(owner.ROPath, bundleInfo.MD5);
                Debug.LogError(path);
                Context.Sys.mono.StartCoroutine(LoadFromUri1(path));
            }

            public override void Update()
            {
                if (State != ELoadState.Loading)
                {
                    return;
                }
            }

            public override void Finish()
            {
                Awaiter?.SetResult();
            }

            public void Dispose()
            {
                State = ELoadState.Invalid;
                if (Ab != null)
                {
                    //AB.Unload(true);
                    Ab.Unload(false);
                    Ab = null;
                }
            }

            public void LoadSync()
            {
                Ab = AssetBundle.LoadFromFile(Name);

                owner.bundles[Name] = this;
                owner.missing.Remove(Name);
            }

            private IEnumerator LoadFromUri1(string path)
            {
                UnityWebRequest www = new UnityWebRequest(path);
                DownloadHandlerAssetBundle handler = new DownloadHandlerAssetBundle(www.uri.ToString(), 0);
                www.downloadHandler = handler;
                yield return www.SendWebRequest();
                if (www.result == UnityWebRequest.Result.Success)
                {
                    Ab = handler.assetBundle;
                    Progress = 1;
                    State = ELoadState.Complete;
                    owner.bundles[Name] = this;
                    // ab.LoadAsset
                    //AB.Unload(false);
                }
                else
                {
                    Debug.LogError(GetType() + "/ERROR/" + www.error);
                }
                www.Dispose();
            }

            private IEnumerator LoadFromUri2(string path)
            {
                UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(path);
                yield return request.SendWebRequest();
                if (request.result == UnityWebRequest.Result.Success)
                {
                    Ab = (request.downloadHandler as DownloadHandlerAssetBundle)?.assetBundle;
                    Progress = 1;
                    State = ELoadState.Complete;
                    owner.bundles[Name] = this;
                    // ab.LoadAsset
                    //AB.Unload(false);
                }
                else
                {
                    Debug.LogError(GetType() + "/ERROR/" + request.error);
                }
                request.Dispose();
            }
        }
    }
}
